package com.junsi.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.junsi.bean.Distribution;
import com.junsi.bean.Jurisdiction;
import com.junsi.bean.Role;
import com.junsi.bean.Staff;
import com.junsi.service.DistributionSerivce;
import com.junsi.service.JurisdictionSerivce;
import com.junsi.service.RoleSerivce;
import com.junsi.service.StaffSerivce;
import com.junsi.util.InteractiveUtil;

@Controller
@RequestMapping("/role")
public class RoleController {
	
	@Autowired
	private RoleSerivce roleSerivce;
	
	@Autowired
	private DistributionSerivce distributionSerivce;
	
	@Autowired
	private JurisdictionSerivce jurisdictionSerivce;
	
	@Autowired
	private StaffSerivce staffSerivce;
	
	/**
	 * 新增角色
	 * @param role
	 * @return
	 */
	@RequestMapping("/save")
	public void save(Role role,HttpServletResponse response){
		
		
		if(role == null){
			InteractiveUtil.WriteStringForHtml("新增失败", response);
			return;
		}
		if(role.getRole_name() == null){
			InteractiveUtil.WriteStringForHtml("新增失败", response);
			return;
		}
		if(role.getRole_jurisdictionId() == null){
			role.setRole_jurisdictionId(1);
		}
		if(role.getRole_remarks().equals("")){
			role.setRole_remarks(null);
		}
		boolean result = roleSerivce.save(role);
		if(result){
			
			InteractiveUtil.WriteStringForHtml("新增成功", response);
			return;
		}
		
		InteractiveUtil.WriteStringForHtml("新增失败", response);
	}
	
	/**
	 * 修改角色
	 * @param role
	 * @return
	 */
	@RequestMapping("/update")
	public void update(Role role,HttpServletResponse response){
		
		if(role == null || role.getRole_id() == null){
			InteractiveUtil.WriteStringForHtml("保存失败", response);
			return;
		}
		boolean b = roleSerivce.update(role);
		if(b){
			InteractiveUtil.WriteStringForHtml("保存成功", response);
		}else{
			InteractiveUtil.WriteStringForHtml("保存失败", response);
		}
		
	}
	
	/**
	 * 查看角色详情
	 * @param id
	 * @return
	 */
	@RequestMapping("/findById")
	public void findById(Integer role_id,HttpServletResponse response){
		if(role_id != null){
			Role role = roleSerivce.findById(role_id);
			if(role != null){
				InteractiveUtil.WriteBeanForHtml(role, response);
			}else{
				InteractiveUtil.WriteStringForHtml("发生未知错误请重新刷新页面！", response);
			}
		}else{
			InteractiveUtil.WriteStringForHtml("发生未知错误请重新刷新页面！", response);
		}
		InteractiveUtil.WriteStringForHtml("发生未知错误请重新刷新页面！", response);
	}
	
	/**
	 * 根据ID删除角色信息
	 * @param id
	 * @return
	 */
	@RequestMapping(value="/delete",produces = "application/json; charset=utf-8")
	public void delete(Integer id,HttpServletResponse response){
		
		if(id != null){
			boolean result = roleSerivce.delete(id);
			if(result){
				InteractiveUtil.WriteStringForHtml("删除成功", response);
			}else{
				InteractiveUtil.WriteStringForHtml("删除失败", response);
			}
		}else{
			InteractiveUtil.WriteStringForHtml("删除失败", response);
		}
		
	}
	
	/**
	 * 进入角色管理界面
	 * @return
	 */
	@RequestMapping("/page")
	public ModelAndView page(){
		ModelAndView modelAndView = new ModelAndView();
		
		List<Role> roles = roleSerivce.findByAll();
		List<Jurisdiction> jurisdictions = jurisdictionSerivce.findByAll();
		modelAndView.addObject("roles", roles);
		modelAndView.addObject("jurisdictions", jurisdictions);
		modelAndView.setViewName("forward:/html/setupgroups.jsp");//--------------------------
		
		return modelAndView;
	}
	
	/**
	 * 进入分配角色页面
	 * @param staff_id
	 * @return
	 */
	@RequestMapping("/roleDistribution")
	public ModelAndView  roleDistribution(List<Integer> id){
		
		ModelAndView modelAndView = new ModelAndView();
		if(id.size()>0){
			List<Staff> staffs = new ArrayList<>();
			Integer i = null;
			for (Integer integer : id) {
				Staff staff = staffSerivce.findById(integer);
				if(staff != null){
					staffs.add(staff);
				}
				++i;
			}
			modelAndView.addObject("list", staffs);
			modelAndView.addObject("number", i);
		}else{
			return modelAndView;
		}
		List<Role> roles = roleSerivce.findByAll();
		modelAndView.addObject("roles", roles);
		
		
		return modelAndView;
	}
	
	@RequestMapping("/updateDistribution")
	@ResponseBody
	public String updateDistribution(List<Integer> id,Integer role_id){
		
		boolean reuslt = distributionSerivce.updateDistribution(role_id, id);
		if(reuslt){
			return "成功";
		}else{
			return "失败";
		}
		
	}
	
	@RequestMapping("/findJurisdiction")
	public void findJurisdiction(Integer role_id){
		if(role_id != null){
			Role role = roleSerivce.findById(role_id);
			role.getRole_id();
		}
	}
	
	@RequestMapping("/findByAll")
	public void findByAll(HttpServletResponse response){
		List<Role> roles = roleSerivce.findByAll();
		InteractiveUtil.WriteJsonForHtml(roles, response);
	}
	
	@RequestMapping("/updateDistributions")
	public void updateDistribution(HttpServletResponse response,String staff_id,Integer role_id){
		
		if(staff_id == null || role_id == null){
			InteractiveUtil.WriteStringForHtml("分配失败", response);
			return;
		}
		
		String[] str = staff_id.split(",");
		Distribution distribution = new Distribution();
		distribution.setDistribution_roleId(role_id);
		for(int i = 0,l = str.length;i < l;i++){
			Integer staId = Integer.valueOf(str[i]);
			distribution.setDistribution_staffId(staId);
			boolean b = distributionSerivce.updateByStrffId(distribution);
			if(!b){
				InteractiveUtil.WriteStringForHtml("分配失败", response);
				return;
			}
		}
		InteractiveUtil.WriteStringForHtml("分配成功", response);
	}
}
